package com.yao.club.controller.goods;

import com.yao.club.entity.vo.ShoppingCartResponseVO;
import com.yao.club.entity.request.AddCarRequestEntity;
import com.yao.club.entity.request.UpdateCartItemRequest;
import com.yao.club.entity.request.DeleteCartItemRequest;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.service.GoodsShoppingCarService;
import com.yao.club.token.UserToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "俱乐部商城-购物车")
@RestController
@RequestMapping("/mall/shoppingCart")
public class GoodsShoppingCarController {

    @Autowired
    private GoodsShoppingCarService goodsShoppingCarService;

    @Resource
    HttpServletRequest request;

    @UserToken
    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("/addShoppingCar")
    public ResponseEntity<Boolean> addToCart(@RequestBody AddCarRequestEntity entity) {
        String username = (String) request.getAttribute("username");
        return goodsShoppingCarService.addToCart(entity,username);
    }

    @UserToken
    @ApiOperation(value = "查询购物车商品数量")
    @GetMapping("/orderShoppingCartNum")
    public ResponseEntity<Integer> getShoppingCartCount() {
        String username = (String) request.getAttribute("username");
        return goodsShoppingCarService.getShoppingCartCount(username);
    }

    @UserToken
    @ApiOperation(value = "查询购物车列表")
    @GetMapping("/orderShoppingCartList")
    public ResponseEntity<ShoppingCartResponseVO> getShoppingCart() {
        String username = (String) request.getAttribute("username");
        return goodsShoppingCarService.getShoppingCart(username);
    }

    @UserToken
    @ApiOperation(value = "更新购物车商品数量")
    @PostMapping("/update")
    public ResponseEntity<Boolean> updateCartItem(@RequestBody UpdateCartItemRequest entity) {
        String username = (String) request.getAttribute("username");
        return goodsShoppingCarService.updateCartItem(entity, username);
    }

    @UserToken
    @ApiOperation(value = "删除购物车商品")
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteCartItem(@RequestBody DeleteCartItemRequest entity) {
        String username = (String) request.getAttribute("username");
        return goodsShoppingCarService.deleteCartItem(entity, username);
    }
} 